<%@ page contentType="text/html;charset=UTF-8" import="java.sql.*" %>
<%
    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/sims";
    String USER = "root";
    String PASS = "123456"; 
    Class.forName(JDBC_DRIVER);
    Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
    String	username = (String) session.getAttribute("username");
    String oldPassword = request.getParameter("oldPassword");
    String newPassword = request.getParameter("newPassword");
    int rowsAffected = 0;
    if (username != null && oldPassword != null && newPassword != null) {
        String checkPasswordSql = "SELECT * FROM users WHERE username = ? AND password = ?";
        PreparedStatement checkPasswordPsmt = conn.prepareStatement(checkPasswordSql);
        checkPasswordPsmt.setString(1, username);
        checkPasswordPsmt.setString(2, oldPassword);
        ResultSet rs = checkPasswordPsmt.executeQuery();
        if (rs.next()) {      
            String updateSql = "UPDATE users SET password = ? WHERE username = ?";
            PreparedStatement updatePsmt = conn.prepareStatement(updateSql);
            updatePsmt.setString(1, newPassword);
            updatePsmt.setString(2, username);
            rowsAffected = updatePsmt.executeUpdate();
        } else {
            out.println("原密码输入错误，请重试。");
        }
    }
%>
<style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f9f9f9;
        }
        h2 {
            color: #333;
        }
        form {
            margin-bottom: 20px;
        }
        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 8px;
            margin: 5px 0;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        a {
            text-decoration: none;
            color: #1e90ff;
        }
        p {
            color: #333;
        }
    </style>
<html>
<head>
    <title>修改密码</title>
</head>
<body>
    <h2>修改密码</h2>
    <form action="modify_pwd.jsp" method="post">
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" value="<%= username %>"><br><br>
        <label for="oldPassword">旧密码:</label>
        <input type="password" id="oldPassword" name="oldPassword"><br><br>
        <label for="newPassword">新密码:</label>
        <input type="password" id="newPassword" name="newPassword"><br><br>
        <input type="submit" value="确认修改">
    </form>
    <a href="login.jsp">返回登录页面</a>
    <a href="index.jsp">返回学生信息页</a>
    <% if (username != null && oldPassword != null && newPassword != null) { %>
        <% if (rowsAffected > 0) { %>
            <p>密码修改成功！</p>
        <% } else { %>
            <p>密码修改失败，请重试。</p>
        <% } %>
    <% } %>
</body>
</html>
<%
    conn.close();
%>